import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";
import "package:ttlock_flutter/ttgateway.dart";
import "package:ttlock_flutter/ttlock.dart";

class SelectWifi extends StatefulWidget {
  const SelectWifi({super.key});

  @override
  State<StatefulWidget> createState() => _SelectWifiState();

}

class _SelectWifiState extends State<SelectWifi> {

  List _wifiList = [];


  void _setWifiList(List list) {
    setState(() {
      _wifiList = list;
    });
  }

  void _getNearbyWifi() {
    try {
      EasyLoading.show();
      TTGateway.getNearbyWifi((finished, wifiList) {
        EasyLoading.dismiss();
        print("士大夫:>>>>>>>>$wifiList");
        if (mounted) {
          _setWifiList(wifiList);
        }
      }, (errorCode, errorMsg) {
        EasyLoading.showToast(errorMsg);
      });
    } catch (error) {
      EasyLoading.showToast(error.toString());
    }
  }

  @override
  void initState() {
    super.initState();
    _getNearbyWifi();
  }


  @override
  void dispose() {
    super.dispose();
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "选择WIFI",
      child: ListView.separated(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        itemBuilder: (context, index) {
          Map wifi = _wifiList[index];
          return GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Stacker.pop(wifi);
            },
            child: DecoratedBox(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6),
                  boxShadow: const [ThemeVars.boxShadow],
                  color: Colors.white,
                ),
                child: Container(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(wifi["wifi"], style: const TextStyle(fontSize: 18),),
                      Text(wifi["rssi"] > -60 ? "信号强" : "信号弱")
                    ],
                  ),
                )
            ),
          );
        },
        separatorBuilder: (context, index) => const SizedBox(height: 12),
        itemCount: _wifiList.length,
      ),
    );
  }

}